package com.three.service.impl;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;
import com.three.entity.Shopkeeperchange;
import com.three.entity.Staff;
import com.three.entity.Stafftransfer;
import com.three.mapper.DepartmentMapper;
import com.three.mapper.ShopkeeperchangeMapper;
import com.three.mapper.StaffMapper;
import com.three.mapper.StafftransferMapper;
import com.three.service.StafftransferService;
import com.three.vo.R;
import com.three.vo.StaffExListVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class StafftransferServiceImpl implements StafftransferService {
    @Autowired
    private StafftransferMapper stafftransferMapper;
    @Autowired
    private ShopkeeperchangeMapper shopkeeperchangeMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private HttpSession session;

    //查询所有调动审核员工
    @Override
    public R getAll(Integer currentPage, Integer pageSize, StaffExListVo staffExListVo) {
        PageHelper.startPage(currentPage, pageSize);//开启分页功能
        List<Stafftransfer> stafftransferList = stafftransferMapper.selectAll(staffExListVo);//调用方法查询所有
        //封装分页数据
        PageInfo<Stafftransfer> pageInfo = new PageInfo<>(stafftransferList);
        return R.ok(pageInfo);
    }

    //通过审核 确认调动员工 通过调动就把状态码改为2 驳回就改成3
    @Override
    @Transactional//事务
    public R updateStateYes(Integer id, Integer newbranchid, Integer newdeptid, Integer staffid,Integer deptid) {//调离id，新分店id，新部门id，平调员工id，旧部门id
        //获取平调人员信息
        Staff staff1 = staffMapper.checkStaffid(staffid);
        Integer roleid1 = staff1.getRoleid();//平调员工的角色
        //从session中获取当前操作的员工信息
        Staff staff = (Staff) session.getAttribute("staff");
        //根据审核表id查询
        Stafftransfer stafftransfer = stafftransferMapper.selectByPrimaryKey(id);
        //修改员工调动信息
        stafftransfer.setState(2);//调动状态改为已调动
        stafftransfer.setExtime(new Date());//审核时间
        stafftransfer.setExid(staff.getStaffid());//审核人id
        //选择性地更新员工调动信息，只更新不为空的字段
        int i = stafftransferMapper.updateByPrimaryKeySelective(stafftransfer);
        //调动成功 新所在部门人数加一
        int newdept = departmentMapper.addCounts(newdeptid);
        //调动成功 旧部门人数减一
        int olddept = departmentMapper.minusCounts(deptid);
        if (i > 0) {
            //根据更新结果返回相应的信息
            int i1 = staffMapper.updateBranchAndDept(staffid, newbranchid, newdeptid);
            if (i1 > 0) {
                //调动成功,在调动信息表中添加数据
                Shopkeeperchange sh=new Shopkeeperchange();//调动信息
                sh.setBranchid(staff.getBranchid());//旧门店
                sh.setDeptid(staff.getDeptid());//旧部门
                sh.setTransferper(staff.getStaffid());//执行人
                sh.setNewbranchid(newbranchid);//新门店
                sh.setNewdeptid(newdeptid);//新部门
                sh.setStaffid(staffid);//平调员工
                sh.setNewroleid(roleid1);//新角色
                sh.setRoleid(roleid1);//旧角色
                sh.setTransfertime(new Date());//调动时间
                int i2 = shopkeeperchangeMapper.insterPingDiao(sh);//调动记录

                return R.ok("调动成功");
            } else {
                return R.error("调动失败");
            }
        } else {
            return R.error("调动失败");
        }
    }


    //驳回审核
    @Override
    public R updateStateNo(Integer id) {
        // 从session中获取当前操作的员工信息
        Staff staff = (Staff) session.getAttribute("staff");
        // 根据ID查询员工调动记录
        Stafftransfer stafftransfer = stafftransferMapper.selectByPrimaryKey(id);
        // 设置调动记录的状态为驳回（状态码3代表驳回）
        stafftransfer.setState(3);
        // 设置驳回的时间为当前日期和时间
        stafftransfer.setExtime(new Date());
        // 设置驳回操作者的ID为当前登录者的ID
        stafftransfer.setExid(staff.getStaffid());
        // 选择性更新员工调动记录，仅更新部分字段
        int i = stafftransferMapper.updateByPrimaryKeySelective(stafftransfer);
        // 根据更新结果返回相应的消息
        if (i > 0) {
            // 更新成功，返回成功消息
            return R.ok("驳回成功");
        }
        // 更新失败，返回错误消息
        return R.error("驳回失败");
    }


}
